package com.audaque.springboot.foshanupload.core.model.qo;


import com.audaque.springboot.foshanupload.core.model.qo.page.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zgb
 * @desc ...
 * @date 2021-12-29 23:44:43
 */

@Schema(description = "基础查询参数")
@Data

@NoArgsConstructor
@AllArgsConstructor

public class BaseQo implements Serializable {
    private static final long serialVersionUID = 1L;
    private IdQo idQo;
    private DynamicQo dynamicQo;

    private List<InstrQo> instrQoList = new ArrayList<>();
    private List<RangQo> rangeQoList = new ArrayList<>();
    private List<InQo> inQoList = new ArrayList<>();
    private LimitQo limitQo;

    public void setLimitQo(Integer offset, Integer limit) {
        if (offset != null && limit != null && offset >= 0 && limit > 0) {
            LimitQo limitQo = new LimitQo();
            limitQo.setPrefix(offset);
            limitQo.setSuffix(limit);
            this.setLimitQo(limitQo);
        }
    }

    public Page toPage() {
        Page page = new Page();
        if (this.limitQo != null) {
            Integer prefix = this.limitQo.getPrefix();
            Integer suffix = this.limitQo.getSuffix();
            page.setPage((prefix / suffix) + 1);
            page.setRows(suffix);
            page.setOffset(prefix);
            page.setLimit(suffix);
        }


        return page;
    }

    public void clearlLimitQo() {
        this.limitQo = null;
    }

    public void setCountWhat() {
        if (this.dynamicQo != null) {
            this.dynamicQo.setCountWhat("*");
        } else {
            this.dynamicQo = new DynamicQo();
            this.dynamicQo.setCountWhat("*");
        }
    }

    public void clearCountWhat() {
        if (this.dynamicQo != null) {
            this.dynamicQo.setCountWhat(null);
        }
    }
}
